package com.sm.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.sm.li.common.page.PageBean;
import com.sm.li.common.valid.group.AddGroup;
import com.sm.li.dto.AdminDTO;
import com.sm.li.entity.Admin;
import com.sm.li.entity.AdminRole;
import com.sm.li.mapper.AdminMapper;
import com.sm.li.mapper.AdminRoleMapper;
import com.sm.li.query.AdminQuery;
import com.sm.li.service.AdminService;
import com.sm.li.service.base.impl.BaseServiceImpl;
import com.sm.li.transfer.AdminTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


//两个实体类的属性值互换   BeanUtils jar包     spring提供了BeanUtils
@Service
@Transactional
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminTransfer adminTransfer;

    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Override
    public PageBean<AdminDTO> searchPage(AdminQuery adminQuery) {
        System.out.println(adminQuery);
        LambdaQueryWrapper<Admin> lambda = new QueryWrapper<Admin>().lambda();
        if (!StringUtils.isEmpty(adminQuery.getAdminName()))
            lambda.like(Admin::getAdminName,adminQuery.getAdminName());

        if (!StringUtils.isEmpty(adminQuery.getAdminPhone()))
            lambda.eq(Admin::getAdminPhone,adminQuery.getAdminPhone());

        if (Objects.nonNull(adminQuery.getGender()))
            lambda.eq(Admin::getGender,adminQuery.getGender());

        if(Objects.nonNull(adminQuery.getStarTime())&&Objects.nonNull(adminQuery.getEndTime()))
            lambda.between(Admin::getCreateTime,adminQuery.getStarTime(),adminQuery.getEndTime());

        List<Admin> admins = adminMapper.selectList(lambda);
//        System.out.println(lambda);
        PageInfo<Admin> pageInfo=new PageInfo<>(admins);
        System.out.println(admins);
        List<AdminDTO> adminDTOS = adminTransfer.toDTO(admins);
        System.out.println(adminDTOS);
        return PageBean.initData(pageInfo.getTotal(),adminDTOS);
    }

    @Override
    public boolean hasAdminWithAccountPhoneEmail(Admin admin) {
        Integer integer = adminMapper.selectCount(new QueryWrapper<Admin>().lambda().or().eq(Admin::getAdminAccount, admin.getAdminAccount()).or().eq(Admin::getAdminPhone, admin.getAdminPhone()).or().eq(Admin::getAdminEmail, admin.getAdminEmail()));
        return integer > 0;
    }

    @Override
    public int addAdminAndRole(Admin admin) {
        this.add(admin);
        if (!CollectionUtils.isEmpty(admin.getRoleIds())){
            admin.getRoleIds().forEach(roleId->adminRoleMapper.insert(new AdminRole(roleId,admin.getId())));
        }
        return 1;
    }

    @Override
    public AdminDTO getAdminAndRolesByAdmin(Long id) {
        Admin admin=this.findById(id);
        AdminDTO adminDTO = adminTransfer.toDTO(admin);
        List<AdminRole> adminRoles = adminRoleMapper.selectList(new QueryWrapper<AdminRole>().lambda().eq(AdminRole::getAdminId, id));
        List<Long> roleIds = adminRoles.stream().map(AdminRole::getRoleId).collect(Collectors.toList());
        adminDTO.setRoleIds(roleIds);
        return adminDTO;
    }

    @Override
    public int updateAdminAndRole(Admin admin) {
        //删除员工所有的角色
        adminRoleMapper.delete(new QueryWrapper<AdminRole>().lambda().eq(AdminRole::getAdminId,admin.getId()));
        if (!CollectionUtils.isEmpty(admin.getRoleIds())){
            admin.getRoleIds().forEach(roleId->adminRoleMapper.insert(new AdminRole(roleId,admin.getId())));
        }
      return this.update(admin);

    }

    @Override
    public int deleteAdminAndRoles(Long id) {
        adminRoleMapper.delete(new QueryWrapper<AdminRole>().lambda().eq(AdminRole::getAdminId,id));
        return this.deleteById(id);
    }

    @Override
    public int batchDeleteAdminAndRole(List<Long> ids) {
        adminRoleMapper.delete(new QueryWrapper<AdminRole>().lambda().in(AdminRole::getAdminId,ids));
        return this.deleteBatchIds(ids);
    }

    //通过员工id拿到所有的角色
    @Override
    public List<Long> getRolesByAdminId(Long id) {
        List<AdminRole> adminRoles = adminRoleMapper.selectList(new QueryWrapper<AdminRole>().lambda().eq(AdminRole::getRoleId, id));
        List<Long> collect = adminRoles.stream().map(AdminRole::getRoleId).collect(Collectors.toList());

        return collect;
    }

    //登陆功能
    @Override
    public Admin getAdminByAccount(String account) {
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().lambda().eq(Admin::getAdminAccount, account));

        return admin;
    }

}
